//
//  ZYComposeTextView.swift
//  WBBeta1
//
//  Created by wustzhy on 16/8/17.
//  Copyright © 2016年 Yestin. All rights reserved.
//

import UIKit

class ZYComposeTextView: UITextView {
    //重写setter方法--设置字体
    override var font: UIFont? {
        didSet{
            placeholderLabel.font = font
        }
    }
    //设置占位文字
    var placeholder: String? {
        didSet{
            placeholderLabel.text = placeholder
        }
    }
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        backgroundColor = UIColor.whiteColor()
        addSubview(placeholderLabel)
        placeholderLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(8)
            make.leading.equalTo(5)
            make.width.equalTo(SCREENW - 10)
        }
        
        //监听系统通知--textView内容改变
        NSNotificationCenter.defaultCenter().addObserver(self, selector:"textViewTextDidChange" , name: UITextViewTextDidChangeNotification, object: nil)
    }
    func textViewTextDidChange() {
        placeholderLabel.hidden = self.hasText()
    }
    //析构函数--移除通知
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    private lazy var placeholderLabel:UILabel = {
       let lab = UILabel()
        lab.textColor = UIColor.grayColor()
        lab.numberOfLines = 0
//        lab.font = UIFont.systemFontOfSize(12)
//        lab.text
        return lab
    }()
    
}
